package mysql

import (
	"shop/userrpc/userrpc"

	"gorm.io/gorm"
)

// 商品

type Shop struct {
	gorm.Model
	ShopName   string  `gorm:"type:varchar(30);not null;comment:商品名称"`
	ShopPrice  float32 `gorm:"type:float;not null;comment:商品价格"`
	ShopCode   int     `gorm:"type:int;not null;comment:商品货号"`
	ShopAuthor string  `gorm:"type:varchar(30);not null;comment:商品供货商"`
	UserId     int     `gorm:"type:int;not null;comment:用户ID"`
}

// 商品添加

func (s *Shop) GoodAdd(db *gorm.DB) error {
	return db.Debug().Create(s).Error
}

// 商品列表

func ShopList(db *gorm.DB, in *userrpc.GoodListRequest) []*userrpc.GoodList {
	var list []*userrpc.GoodList

	db.Model(&User{}).
		Select(
			"users.id as UserId",
			"users.username as Username",
			"users.`password` as Password",
			"shops.shop_name as ShopName",
			"shops.shop_price as ShopPrice",
			"shops.shop_code as ShopCode",
			"shops.shop_author as ShopAuthor",
		).
		Joins("left join shops on shops.user_id = users.id").
		Debug().Where("shops.user_id=?", in.UserId).Find(&list)

	return list
}
